<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Admin\Model\AdminUserModel;
use Admin\Model\CategoryModel;
use Admin\Model\UserModel;
class UserController extends AdminController
{
    //用户列表$this->checkPower();
    public function index()
    {
        $this->checkPower();
        $name = "";
        if(!empty($_GET['name'])){
            $name = I("get.name");
        }
        $group = "";
        if(!empty($_GET['group'])){
            $group= I("get.group");
        }
        $category = "";
        if(!empty($_GET['category'])){
            $category = I("get.category");
        }
        $data['name'] = $name;
        $data['group'] = $group;
        $data['category'] = $category;

        //排序字段
        $order = "";
        if(!empty($_GET['order'])){
            $order = I("get.order");
        }
        //排序方式
        $order1 = "";
        if(!empty($_GET['order1'])){
            $order1 = I("get.order1");
        }
        $data['order'] = $order;
        $data['order1'] = $order1;
        $this->title = "用户列表";
        $user = new UserModel();
        $res = $user -> queryAllUser($data);
        $this->userList = $res['list'];
        //分页
        $this->page = $res['show'];
        //用户类型
        $cate = new CategoryModel();
        $cateList = $cate->queryCate();
        $this->assign("cateList",$cateList);

        //用户分组
        $group = M("group");
        $groupList = $group -> select();
        $this->assign("groupList",$groupList);
        //上一级路径
        $this ->assign("prevUrl",$_SERVER['HTTP_REFERER']);
        $this->display("User/index");
    }

    //用户详情页
    public function userDetail()
    {
        $this->title = "用户详情页";
        $user = new UserModel();
        $userInfo = $user -> queryUser(I('get.id'));
        $this->userInfo = $userInfo;
        //上一级路径
        $this ->assign("prevUrls",U("User/index"));
        $this->display("User/userDetail");
    }

    //用户详情编辑页$this->checkPower();
    public function userEdit ()
    {
        $this->checkPower();
        $this->title = "用户详情编辑页";
        $user = new UserModel();
        $userInfo = $user -> queryUser(I('get.id'));
        $this->userInfo = $userInfo;

        //查询所有的职位
        $cate = new CategoryModel();
        $cateList = $cate->queryAllCate();
        $this->cateList = $cateList;

        //用户分组
        $group = M("group");
        $groupList = $group -> select();
        $this->assign("groupList",$groupList);

        //上一级路径
        $this ->assign("prevUrl",$_SERVER['HTTP_REFERER']);
        $this->display("User/userEdit");
    }

    //用户详情编辑保存
    public function saveUser ()
    {

        $id = I('post.id');
        $category = I('post.category');
        $status = I('post.status');
        $fee_time = I('post.fee_time');
        $card_num = I('post.card_num');
        $tel = I('post.tel');
        if(empty($fee_time)){
            $fee_time = "0000-00-00 00:00:00";
        }else{
            $fee_time = date("Y-m-d H:i:s",strtotime($fee_time));
        }
        if(!empty($_POST['join_time'])){
            $join_time = I('post.join_time');
            $join_time = date("Y-m-d H:i:s",strtotime($join_time));
            $data['join_time'] = $join_time;
        }
        if(!empty($_POST['group'])){
            $group_id = I('post.group');
            $data['group_id'] = $group_id;
        }

        //保存用户信息
        $data['status'] = $status;
        $data['fee_time'] = $fee_time;
        $data['card_num'] = $card_num;
        $data['tel'] = $tel;
        $where['id'] = $id;
        $user = new UserModel();
        $res = $user->saveUser($data,$where);
        if($res > 0){
            //修改用户类型
            $user_category = M("user_category");
            $cateData['category_id'] = $category;
            $cateData['update_time'] = date("Y-m-d H:i:s");
            $user_category -> where("user_id=$id")->save($cateData);
//            $this->success("修改成功",U("User/index"));
            $this->redirect("User/index");
        }else{
            $this->error("修改失败");
        }


    }

    //添加管理员$this->checkPower();
    public function add ()
    {
        $this->checkPower();
        $this->assign("title","添加管理员");
        //上一级路径
        $this ->assign("prevUrl",$_SERVER['HTTP_REFERER']);
        $this->display("User/add");
    }

    //添加管理员到数据库
    public function addDo ()
    {
        $pwd = I("post.pwd");
        $repwd = I("post.repwd");
        if($pwd != $repwd){
            $this->error("两次密码输入不一致");
        }

        $name = I("post.name");
        //添加管理员前,要看看该管理员是否已经存在
        $user = new AdminUserModel();
        $res = $user->queryUser($name);
        if($res > 0){
            $this->error("该管理员已存在");
        }

        $data['name'] = $name;
        $data['password'] = md5($pwd);
        $data['sort_id'] = 2;
        $result = $user->addUser($data);
        if($result > 0){
            $this->redirect("User/adminUserList");
        }else{
            $this->error("添加管理员失败");
        }
    }

    //管理员列表$this->checkPower();
    public function adminUserList ()
    {
        $this->checkPower();
        $name = "";
        if(!empty($_GET['name'])){
            $name = I("get.name");
        }
        $data['name'] = $name;
        $this -> assign("管理员列表");
        //查询所有的管理员
        $user = new AdminUserModel();
        $res = $user->queryAllAdminUser($data);
        $adminUserList = $res['list'];
        $show = $res['show'];
        $this -> assign("adminUserList",$adminUserList);
        //分页
        $this -> assign("page",$show);
        $this -> display("User/adminUser");
    }

    //用户权限类型管理
    public function userCatePower ()
    {
        $this->assign("title","用户权限类型列表");
        $cate = new CategoryModel();
        $cateList = $cate->queryAllCate();
        $this->assign("cateList",$cateList);
        $this->display("User/userCatePower");
    }

    //添加用户$this->checkPower();
    public function addUser()
    {
        $this->checkPower();
        $this->title = "添加党员";
        //查询所有党员类型
        $cate = new CategoryModel();
        $cateList = $cate->queryCate();
        $this->assign("cateList",$cateList);
        //用户分组
        $group = M("group");
        $groupList = $group -> select();
        $this->assign("groupList",$groupList);
        $this->assign("prev",$_SERVER['HTTP_REFERER']);
//        var_dump($groupList);die;
//        echo "<pre>";print_r($_SERVER);die;
        $this->display("User/addUser");
    }

    //保存用户数据到数据库
    public function addUsers ()
    {
//        echo "<pre>";
//        print_r(I("post."));
        $data['name'] = I("post.name");
        $data['sex'] = I("post.sex");
        $data['password'] = md5("123456");
        $data['status'] = I("post.status");
        $data['tel'] = I("post.tel");
        $data['card_num'] = I("post.card_num");
        $data['create_time'] =date("Y-m-d H:i:s");
        if(empty($_POST['join_time'])){
            $data['join_time'] = "";
        }else{
            $data['join_time'] = date("Y-m-d H:i:s",strtotime(I("post.join_time")));
        }
        if(empty($_POST['fee_time'])){
            $data['fee_time'] = "";
        }else{
            $data['fee_time'] = date("Y-m-d H:i:s",strtotime(I("post.fee_time")));
        }
        $data['level'] = 1;
        if(empty($_POST['group'])){
            $data['group_id'] = "";
        }else{
            $data['group_id'] = I("post.group");
        }

        $user = new UserModel();
        $res = $user -> addUser($data);
//        var_dump($res);die;
        if($res > 1){
            $user_category = M("user_category");
            $datas['user_id'] = $res;
            $datas['category_id'] = I("post.category");
            $user_category->add($datas);
            $this->redirect("User/index");
        }else{
            $this->error("添加失败");
        }
    }

    //权限列表
    public function power()
    {
        $this->checkPower();
        $map = array();
        $name = "";
        if(!empty($_GET['action'])){
            $action = I("get.action");
            $map['p.action'] = array("LIKE", '%' . $action . '%');
        }
        $module = "";
        if(!empty($_GET['module'])){
            $module= I("get.module");
            $map['p.module_id'] = $module;
        }

        //排序字段
        $order = "p.id";
        if(!empty($_GET['order'])){
            $order = I("get.order");
        }
        //排序方式
        $order1 = "asc";
        if(!empty($_GET['order1'])){
            $order1 = I("get.order1");
        }

        $this -> title = "权限列表";
        $power = M("admin_user_power");
        //查询总记录数
        $count = $power->alias("p")->join("dangjian_module m on m.id=p.module_id")->where($map)
            ->count();
        //使用分页
        $page = new \Think\Page($count,10);
        $show = $page->show();

        $list = $power->alias("p")->join("dangjian_module m on m.id=p.module_id")
            ->field("p.id,m.module,p.action,p.path,p.create_time,p.update_time")
            ->where($map)->order("$order $order1")
            ->limit($page->firstRow.','.$page->listRows)
            -> select();
        $this->assign("powerList",$list);
        $this->assign("page",$show);
        //查询所有模块
        $module = M("module");
        $res = $module -> select();
        $this->assign("moduleList",$res);
        $this->display("User/power");
    }

    //普通管理员权限管理$this->checkPower();
    public function sortPower()
    {


        $this->checkPower();
        $this -> title = "普通管理员权限管理";
        $power = M("admin_user_power");
        $res = $power-> field("id,module_id,action,path") -> select();
        $this->assign("powerList",$res);
        //模块列表
        $module = M("module");
        $res = $module -> select();
        $this->assign("moduleList",$res);
        //查看普通管理员的权限
        $sort = M("admin_user_sort");
        $res = $sort->field("power")->where("id=2")->find();
        $power = $res['power'];
        $power = explode(",",$power);
        $this->assign("power",$power);
        $this->display("User/sortPower");
    }

    public function saveUserPower()
    {

        $power = implode(array_keys(I("post.")),",");
        $sort = M("admin_user_sort");
        $sort->where("id=2")->save(array("power"=>$power));
        $this->redirect("User/sortPower");
    }

    public function deleteAdminUser (){
        $this->checkPower();
        $id = I("get.id");
        $com = new AdminUserModel();
        $com->deleteAdminUser($id);
        $this->redirect("User/adminUserList");
    }
}